package fr.istic.linkedcv.client.presentation.views.impl;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import fr.istic.linkedcv.client.presentation.impl.UiImpl;
import fr.istic.linkedcv.client.presentation.views.View;
import fr.istic.linkedcv.client.services.PersonneService;
import fr.istic.linkedcv.client.services.PersonneServiceAsync;

public class LoginForm extends View {

	private TextBox login = new TextBox();
	private PasswordTextBox password = new PasswordTextBox();
	private Label idLabel = new Label("Identifiant : ");
	private Label pwdLabel = new Label("Password : ");
	private Label inscriptionLabel = new Label("s'inscrire");
	private Hyperlink inscriptionLink = new Hyperlink("s'inscrire","inscription");
	private Button submitBtn = new Button("Se connecter");
	private PersonneServiceAsync personneService = GWT.create(PersonneService.class);;
	private Grid grid = new Grid(3,2);
	public LoginForm(UiImpl ui) {
		super(ui);
		
	    
	    	
	    
	   
	}

	public void build() {
		submitHandler();
//		inscriptionLabel.addClickHandler(new ClickHandler() {
//			
//			@Override
//			public void onClick(ClickEvent event) {
//				ui.displayContent("inscription");
//				
//			}
//		});
		grid.setWidget(0, 0, idLabel);
		grid.setWidget(0, 1, login);
		grid.setWidget(1, 0, pwdLabel);
		grid.setWidget(1, 1, password);
		grid.setWidget(2, 0, submitBtn);
		grid.setWidget(2, 1, inscriptionLink);
		grid.setHeight("90px");
		grid.setWidth("300px");
		grid.addStyleName("login_form");
	}

	public void submitHandler() {
		submitBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (login.getValue().isEmpty()) {
					Window.alert("Le champ identifiant doit être saisi.");
				} else if (password.getValue().isEmpty()) {
					Window.alert("Le champ password doit être saisi.");
				} else
					login();

			}
		});
	}

	protected void login() {
		Map<String, String> personneMap = new HashMap<String, String>();
		personneMap.put("identifiant", login.getValue());
		personneMap.put("password", password.getValue());
		
		personneService.login(personneMap, new AsyncCallback<Boolean>(){

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("failure");
				
			}

			@Override
			public void onSuccess(Boolean result) {
				if(result){
					
					ui.getControle().setLogged(true);
					ui.getControle().initPersonneCli();
					ui.addHistoryItem("liste_personnes");
				}else
					Window.alert("Echec de l'authentification");
				
			}
			
		});
	}

	@Override
	public Widget getWidget() {
		return grid;
	}
}
